#pragma once
#include <graphic/intype.h>
#include <graphic/boudingbox.h>
#include <renderer/color.h>
#include <renderer/material.h>


BEGIN_NAMESPACE_OXGE

class Texture;
class Material;

class TextureChannel
{
public:
	TextureChannel();
	Vector3IndexArray m_vTexFaces;
	Vector2Array m_vTexCoords;

	Vector2& GetFaceVertexCoord(int iface, int ivertex);
	const Vector2& GetFaceVertexCoord(int iface, int ivertex)const;

	Texture* pTexture;
};


//geometry class 
class RENDERER_TRANSFER SceneObject : public oxgeClass<SceneObject> 
{
public:

	SceneObject();

	SceneObject& SetMaterialFront(Material*pMat){ m_pMatFront = pMat; return *this; }
	SceneObject& SetMaterialBack(Material* pMat){ m_pMatBack = pMat; return *this; }
	Material* GetMaterialFront(){ return m_pMatFront.get(); }
	Material* GetMaterialBack(){ return m_pMatBack.get(); }

	SceneObject& SetColor(const ColorRGBA& color){ m_color = color; return *this; }
	inline ColorRGBA GetColor()const{ return m_color; }
	//geometry data
	Point3Array m_vVertexs;
	Vector3IndexArray m_vFaces;
	
	int AddPoint(const Point3& pt){ m_vVertexs.push_back(pt); return (int)m_vVertexs.size()-1; }
	int AddFace(int p1Index, int p2Index, int p3Index){ m_vFaces.push_back(Vector3Index(p1Index,p2Index,p3Index)); return (int)m_vFaces.size()-1; }
	
 
	Vector3Array m_vNormPerVertex;
	Vector3Array m_vNormPerFace;	

	enum NormMode{ NormPerVertex, NormPerFace, };
	SceneObject& SetNormMode(NormMode mode){ m_normMode = mode; return *this; }
	SceneObject& RefreshNorms();
	NormMode GetNormMode()const{ return m_normMode; }

	int GetTexChannelCount()const{ return (int)m_vTextureChannels.size(); }
	
	void RefreshBoundingBox();

	bool IsValid()const;
	bool TryFix();

public:
	std::vector<TextureChannel> m_vTextureChannels;

	NormMode m_normMode;

	Material::RefPtr m_pMatFront;
	Material::RefPtr m_pMatBack;

	BoundingBox m_boundbox;
	ColorRGBA m_color; //use this color when no light 

protected:	
	void RefreshNormsPerVertex();
	void RefreshNormsPerFaces();

	bool IsFacesValid()const;
	bool TryFixFaces();
	bool IsNormsValid()const;
	bool TryFixNorms();
	bool IsTextureChannelValid(int idx)const;
	bool TryFixTextureChannel(int idx);

};

END_NAMESPACE_OXGE